package com.authService.service.impl;

import com.authService.entity.SysMenuPermissionEntity;
import com.authService.mapper.SysMenuPermissionMapper;
import com.authService.pojo.req.MenuPermissionSaveReqDTO;
import com.authService.pojo.res.MenuPermissionRespDTO;
import com.authService.service.ISysMenuPermissionService;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 系统菜单权限关联表 服务实现类
 * </p>
 *
 * @author xiaopeng
 * @since 2024-10-20
 */
@Service
public class SysMenuPermissionServiceImpl extends ServiceImpl<SysMenuPermissionMapper, SysMenuPermissionEntity> implements ISysMenuPermissionService {

    @Override
    public List<MenuPermissionRespDTO> selectMenuPermissionList(Long menuId) {
        return baseMapper.selectMenuPermissionList(menuId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMenuPermission(MenuPermissionSaveReqDTO reqDTO) {
        LocalDateTime now = LocalDateTime.now();
        Long menuId = reqDTO.getMenuId();
        List<Long> newPermissionIdList = reqDTO.getPermissionIdList();
        List<SysMenuPermissionEntity> oldPermissionlist = this.lambdaQuery().eq(SysMenuPermissionEntity::getMenuId, menuId).list();
        List<Long> menuPermissionIdList = oldPermissionlist.stream().map(SysMenuPermissionEntity::getPermissionId).toList();

        // 新增的菜单权限
        List<Long> addPermissionIdList = newPermissionIdList.stream().filter(e -> !menuPermissionIdList.contains(e)).toList();
        // 要移除的菜单权限
        List<SysMenuPermissionEntity> deletePermissionList = oldPermissionlist.stream().filter(e -> !newPermissionIdList.contains(e.getPermissionId())).toList();

        // 新增
        if (CollectionUtils.isNotEmpty(addPermissionIdList)) {
            List<SysMenuPermissionEntity> addPermissionList = Lists.newArrayList();
            for (Long permissionId : addPermissionIdList) {
                SysMenuPermissionEntity addEntity = new SysMenuPermissionEntity();
                addEntity.setId(IdWorker.getId());
                addEntity.setMenuId(menuId);
                addEntity.setPermissionId(permissionId);
//                addEntity.setCreateBy(UserHolder.getUserId());
//                addEntity.setCreateByName(UserHolder.getUserName());
//                addEntity.setCreateTime(now);
//                addEntity.setUpdateBy(UserHolder.getUserId());
//                addEntity.setUpdateByName(UserHolder.getUserName());
                addEntity.setUpdateTime(now);
                addPermissionList.add(addEntity);
            }
            this.saveBatch(addPermissionList);
        }

        // 移除
        if (CollectionUtils.isNotEmpty(deletePermissionList)) {
            for (SysMenuPermissionEntity deleteEntity : deletePermissionList) {
//                deleteEntity.setUpdateBy(UserHolder.getUserId());
//                deleteEntity.setUpdateByName(UserHolder.getUserName());
                deleteEntity.setUpdateTime(now);
            }
            this.updateBatchById(deletePermissionList);
            this.removeBatchByIds(deletePermissionList);
        }

    }
}
